package top.datacluster.basic.algorithm.leetcode.queue;

import java.util.LinkedList;

public class LeetCode560 {

    public int subarraySum(int[] nums, int k) {

        LinkedList<Integer> queue = new LinkedList<>();
        int num = 0;
        int result = 0;
        for (int i : nums){

            queue.addLast(i);
            num += i;
            while (num > k){
                int temp = queue.removeFirst();
                num -= temp;
            }
            if (num == k && !queue.isEmpty()){
                result++;
                int temp = queue.removeFirst();
                num -= temp;
            }
        }
        while (!queue.isEmpty()){
            int temp = queue.removeFirst();
            num -= temp;
            if (num == k && num != 0){
                result++;
            }
        }
        return result;

    }

    public static void main(String[] args) {
        int[] test = new int[]{-1, -1, 1};
        System.out.println(new LeetCode560().subarraySum(test, 0));
    }
}
